package com.victor.netty.firstexample;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Created by DJJ on 2019/5/31.
 */
public class TestServer {

    public static void main(String[] args) throws Exception {
        //就是一个死循环,boss直接收请求不出来,一个EventLoopGroup 也能完成,netty推荐用两个
        EventLoopGroup bossGruop=new NioEventLoopGroup();
        //处理boss接收的请求,进行处理
        EventLoopGroup wokerGruop=new NioEventLoopGroup();

        try {
            //启动服务端的类
            ServerBootstrap serverBootstrap=new ServerBootstrap();
            serverBootstrap.group(bossGruop,wokerGruop).channel(NioServerSocketChannel.class)
                    //初始化器
                    .childHandler(new TestServerInitializer());
            ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();//sync表示Future的这个动作一定完成了
            channelFuture.channel().closeFuture().sync();//当服务端真正关闭了才能往下走,不然阻塞
        } finally {
            //关闭
            bossGruop.shutdownGracefully();
            wokerGruop.shutdownGracefully();
        }

    }
}
